
export function useTreeData() {
    const processTreeData = (data) => {
        const baseNodeWidth = 100
        const baseNodeSpacing = 20
        const baseNodeHeight = 110

        const calculateRequiredWidth = (node) => {
            if (!node.children || node.children.length === 0) {
                return baseNodeWidth
            }

            const childrenWidth = node.children.reduce((total, child) => {
                return total + calculateRequiredWidth(child)
            }, 0)

            return Math.max(baseNodeWidth, childrenWidth + (node.children.length - 1) * baseNodeSpacing)
        }

        const processNode = (node, level = 0, offsetX = 0) => {
            const result = {
                id: node.id,
                label: node.meter_code,
                spec: node.specification || '',
                supply_region: node.supply_region || '',
                level,
                x: offsetX,
                y: level * (baseNodeHeight + 20),
                isMeter: true,
                width: calculateRequiredWidth(node)
            }

            if (node.children && node.children.length > 0) {
                let currentX = offsetX - (result.width / 2)
                result.children = node.children.map(child => {
                    const childWidth = calculateRequiredWidth(child)
                    const childNode = processNode(
                        child,
                        level + 1,
                        currentX + (childWidth / 2)
                    )
                    currentX += childWidth + baseNodeSpacing
                    return childNode
                })
            }

            return result
        }

        let totalWidth = 0
        let nodes = data.map(node => {
            const width = calculateRequiredWidth(node)
            totalWidth += width
            return { node, width }
        })

        totalWidth += (data.length - 1) * baseNodeSpacing

        let currentX = -totalWidth / 2
        return nodes.map(({ node, width }) => {
            const result = processNode(node, 0, currentX + width / 2)
            currentX += width + baseNodeSpacing
            return result
        })
    }

    const wrapText = (selection, text, width, x = 20, lineHeight = 16) => {
        let words = text.split('')
        let line = ''
        let lineNumber = 0

        words.forEach(char => {
            let testLine = line + char
            if (testLine.length * 13 > width) {
                selection.append("tspan")
                    .attr("x", x)
                    .attr("dy", lineNumber === 0 ? 0 : lineHeight)
                    .text(line)
                line = char
                lineNumber++
            } else {
                line = testLine
            }
        })

        selection.append("tspan")
            .attr("x", x)
            .attr("dy", lineNumber === 0 ? 0 : lineHeight)
            .text(line)

        return lineNumber + 1
    }

    return { processTreeData, wrapText }
}
